package com.dapeng.www.recycletest;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/17.
 */
public class TablelayoutActivity extends FragmentActivity implements  TabLayout.OnTabSelectedListener{

    private FragmentManager fragmentManager;
    private PrepareLessonFragement prepareLessonFragement;//导学界面
    private TeachingFragement teachingFragement;//授课界面
    private HomeWorkFragement homeWrokFragement;// 作业界面
    private TabLayout mTabLayout;//导航栏
    private List<String> mMenuTitles = new ArrayList<String>(); //导航栏数据集合


    private static final String MEUN_PREPARELESSON = "备课";
    private static final String MEUN_TEACHING = "授课";
    private static final String MEUN_HOMEWORK = "作业";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tablelayout);

        fragmentManager = getSupportFragmentManager();

        mTabLayout = (TabLayout) findViewById(R.id.tl_mainActivity_topMenu);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式

        List<String> materialMenus = new ArrayList<>();
        materialMenus.add("备课");
        materialMenus.add("授课");
        materialMenus.add("作业");

        mMenuTitles.addAll(materialMenus);

        initTableMenus();
    }



    /*初始化并加载导航栏
     */
    private void initTableMenus() {
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(0, false)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(1, true)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(2, false)));
        mTabLayout.setOnTabSelectedListener(this);
        mTabLayout.getTabAt(1).select();
    }



    /**
     * 初始化顶部菜单
     * @param position
     * @param selected
     * @return
     */
    private View getTabView(int position, boolean selected) {
        View view = LayoutInflater.from(this).inflate(R.layout.uet_activity_main_top_menu_item, null);
        final TextView txt_title = (TextView) view.findViewById(R.id.tv_main_menu_title);
        final ImageView imageView = (ImageView) view.findViewById(R.id.img);
        imageView.setImageResource(R.drawable.drawable_teaching);
        if (selected) {
            txt_title.setTextColor(getResources().getColorStateList(R.color.uet_color_ff5402));
            imageView.setSelected(true);
        } else
        txt_title.setText(mMenuTitles.get(position));
        return view;
    }


    /**
     * 定义选中当前fragement
     *
     * @param index
     */
    public void selectCurrentFragement(String index) {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case MEUN_PREPARELESSON:// 导学
                if (prepareLessonFragement == null) {
                    // 如果prepareLessonFragement为空，则创建一个并添加到界面上
                    prepareLessonFragement = new PrepareLessonFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, prepareLessonFragement);
                } else {
                    transaction.show(prepareLessonFragement);
                }
                break;

            case MEUN_TEACHING://授课
                if (teachingFragement == null) {
                    // 如果TeachingFragement为空，则创建一个并添加到界面上
                    teachingFragement = new TeachingFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, teachingFragement);
                } else {
                    transaction.show(teachingFragement);
                }
                break;
            case MEUN_HOMEWORK://作业
                if (homeWrokFragement == null) {
                    // 如果HomeWrokFragement为空，则创建一个并添加到界面上
                    homeWrokFragement = new HomeWorkFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, homeWrokFragement);
                } else {
                    transaction.show(homeWrokFragement);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 隐藏所有的Fragment,避免fragment混乱
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (prepareLessonFragement != null) {
            transaction.hide(prepareLessonFragement);
        }
        if (teachingFragement != null) {
            transaction.hide(teachingFragement);
        }
        if (homeWrokFragement != null) {
            transaction.hide(homeWrokFragement);
        }
    }




    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView txt_title = (TextView) view.findViewById(R.id.tv_main_menu_title);
        txt_title.setTextColor(getResources().getColor(R.color.uet_color_ff5402));
        TextView txc = (TextView) view.findViewById(R.id.tv_main_menu_title_count);
        if (txt_title.getText().toString().trim().equals("备课")) {
            selectCurrentFragement(MEUN_PREPARELESSON);
        } else if (txt_title.getText().toString().trim().equals("授课")) {
            selectCurrentFragement(MEUN_TEACHING);
        } else if (txt_title.getText().toString().trim().equals("作业")) {
            selectCurrentFragement(MEUN_HOMEWORK);
        }

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView txt_title = (TextView) view.findViewById(R.id.tv_main_menu_title);
        txt_title.setTextColor(getResources().getColorStateList(R.color.uet_color_262b33));
        ImageView img = (ImageView) view.findViewById(R.id.img);
        img.setSelected(false);
        TextView txCount = (TextView) view.findViewById(R.id.tv_main_menu_title_count);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
